<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-input
        placeholder="请输入内容"
        v-model="queryInfo.query"
        class="input-with-select"
        clearable
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="getgoodList()"
        ></el-button>
      </el-input>

      <el-button
        class="bbb"
        type="primary"
        @click="goAddPage()"
      >添加商品</el-button>

      <template>
        <el-table
          :data="goodList"
          border
          style="width: 100%"
        >
          <el-table-column type="index"></el-table-column>
          <el-table-column
            prop="goods_name"
            label="商品名称"
            width="660px"
          >
          </el-table-column>
          <el-table-column
            prop="goods_price"
            label="商品价格"
            width="100px"
          >
          </el-table-column>
          <el-table-column
            prop="goods_weight"
            label="商品重量"
            width="100px"
          >
          </el-table-column>
          <el-table-column
            prop="add_time"
            label="创建时间"
          >
            <template slot-scope="scope">
              {{scope.row.add_time | dateFormat}}
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <!-- 编辑 -->
              <el-button
                type="primary"
                icon="el-icon-edit"
              ></el-button>
              <!-- 删除 -->
              <el-button
                type="danger"
                icon="el-icon-delete"
                @click="shanchu(scope.row.goods_id)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.total"
        background
      >
      </el-pagination>
    </el-card>

  </div>
</template>
<script>
export default {
  data() {
    return {
      input3: "",
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      goodList: [],
      total: 0,
    };
  },
  created() {
    this.getgoodList();
  },
  mounted() {},
  methods: {
    //获取所有商品
    async getgoodList() {
      const res = await this.$http.get("goods", {
        params: this.queryInfo,
      });
      this.goodList = res.data.data.goods;
      this.total = res.data.data.total;
      // console.log(this.goodList);
      // console.log(res.data);
    },
    //分页
    handleSizeChange(val) {
      this.queryInfo.pagesize = val;
      this.getgoodList();
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val;
      this.getgoodList();
      // console.log(`当前页: ${val}`);
    },

    //删除
    async shanchu(id) {
      this.$confirm("此操作将删除该分类, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await this.$http.delete(`goods/${id}`);
          // console.log(res.data);
          this.getgoodList();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //添加商品跳转页面
    goAddPage() {
      this.$router.push("/add");
    },
  },
};
</script>

<style scoped>
.el-card {
  margin-top: 15px;
}
.input-with-select {
  width: 400px;
}
.bbb {
  margin: 20px;
}
.el-pagination {
  margin-top: 15px;
}
</style>
